﻿using System;
using System.Collections.Generic;
using System.Text;
using LuaInterface;

using JEng_Mark_1.GameComponents;
using JEng_Mark_1.Managers;
using JEng_Mark_1.Textures;
using JEng_Mark_1.GUI;
using JEng_Mark_1.Models;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using JEng_Mark_1.SceneObject;
using Microsoft.Xna.Framework.Input;
using JEng_Mark_1.StandardObjects;
using JEng_Mark_1.Shaders;

namespace JEng_TechDemo
{
    public class MainMenuScreen : JGameScreen
    {
        private JTexture _background = null;
        private JGPanel _consolePanel;
        private static double delta = 0.0;
        private JGridObject _grid;

        /// <summary>
        /// Construct main menu screen and set transition times
        /// </summary>
        public MainMenuScreen()
        {
            TransitionTime = TimeSpan.FromSeconds(0.0f);
            TransitionOffTime = TimeSpan.FromSeconds(0.5f);

            _background = new JTexture("Content/Textures/background");
        }

        public override void LoadContent()
        {
            base.LoadContent();

            //Load the textures
            JTextureManager.AddTexture(_background, "background");

            //Setup the gui panel from console images
            JTexture tlP = new JTexture("Content/GUI/Console/tl_post");
            JTexture trP = new JTexture("Content/GUI/Console/tr_post");
            JTexture brP = new JTexture("Content/GUI/Console/br_post");
            JTexture blP = new JTexture("Content/GUI/Console/bl_post");
            JTextureManager.AddTexture(tlP,"tl_post");
            JTextureManager.AddTexture(trP,"tr_post");
            JTextureManager.AddTexture(brP,"br_post");
            JTextureManager.AddTexture(blP,"bl_post");
            JTexture tB = new JTexture("Content/GUI/Console/tbord");
            JTexture rB = new JTexture("Content/GUI/Console/rbord");
            JTexture bB = new JTexture("Content/GUI/Console/bbord");
            JTexture lB = new JTexture("Content/GUI/Console/lbord");
            JTextureManager.AddTexture(tB,"t_bord");
            JTextureManager.AddTexture(rB,"r_bord");
            JTextureManager.AddTexture(bB,"b_bord");
            JTextureManager.AddTexture(lB,"l_bord");

            //Setup the gui panel from regular panel images
            /*JTexture tlP = new JTexture("Content/GUI/Panel/tlpost");
            JTexture trP = new JTexture("Content/GUI/Panel/trpost");
            JTexture brP = new JTexture("Content/GUI/Panel/brpost");
            JTexture blP = new JTexture("Content/GUI/Panel/blpost");
            JTextureManager.AddTexture(tlP, "tl_post");
            JTextureManager.AddTexture(trP, "tr_post");
            JTextureManager.AddTexture(brP, "br_post");
            JTextureManager.AddTexture(blP, "bl_post");
            JTexture tB = new JTexture("Content/GUI/Panel/tborder");
            JTexture rB = new JTexture("Content/GUI/Panel/rborder");
            JTexture bB = new JTexture("Content/GUI/Panel/bborder");
            JTexture lB = new JTexture("Content/GUI/Panel/lborder");
            JTextureManager.AddTexture(tB, "t_bord");
            JTextureManager.AddTexture(rB, "r_bord");
            JTextureManager.AddTexture(bB, "b_bord");
            JTextureManager.AddTexture(lB, "l_bord");*/

            _consolePanel = new JGPanel(tlP,trP,brP,blP,tB,rB,bB,lB);

            //setup camera
            JFirstPersonCamera camera = new JFirstPersonCamera();
            camera.NearPlane = 10.0f;
            camera.FarPlane = 100000.0f;
            camera.Position = new Vector3(0, 0, 0);//-10000);

            JCameraManager.AddCamera(camera, JCameraManager.CameraNumber._3);
            JCameraManager.SetActiveCamera(JCameraManager.CameraNumber._3);
            JCameraManager.SetAllCamerasProjectionMatrix((float)JEngineManager.Device.Viewport.Width /
                JEngineManager.Device.Viewport.Height);
            JEngineManager.CameraManager.Update(null);


            //setup test model
            JModel ship = new JModel("Content/Models/Ship");//room_full");
            JModelManager.AddModel(ship, "Ship");
            JSceneObject obj = new JSceneObject();
            obj.ModelName = "Ship";
            obj.Position = Vector3.Zero;
            //obj.Scale = new Vector3(500.0f);
            //JSceneGraphManager.AddObject(obj);

            Teapot teapot = new Teapot();
            JSceneGraphManager.AddObject(teapot);

            //setup grid
            _grid = new JGridObject(Color.Red, 100, 500f);
            JSceneGraphManager.AddObject(_grid);

            JSceneGraphManager.LoadContent();

            JEngineManager.Game.ResetElapsedTime();            
        }

        public override void UnloadContent()
        {
            base.UnloadContent();

            //Unload the textures
            JTextureManager.RemoveTexture("background");

            JSceneGraphManager.UnloadContent();
        }

        public override void Update(GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen)
        {
            base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);
            delta = gameTime.ElapsedGameTime.TotalSeconds;
        }

        public override void HandleInput(JInput input)
        {
            base.HandleInput(input);

            //check if the console should be shown
            if (input.IsNewKeyPress(Keys.OemTilde))
            {
                StartConsole();
            }
            if (input.CurrentKeyboardState.IsKeyDown(Keys.Q))
            {
                JCameraManager.ActiveCamera.Translate(new Vector3(0, 3000f * (float)delta, 0.0f));
            }
            if (input.CurrentKeyboardState.IsKeyDown(Keys.Z))
            {
                JCameraManager.ActiveCamera.Translate(new Vector3(0, -3000f * (float)delta, 0.0f));
            }
            if (input.CurrentKeyboardState.IsKeyDown(Keys.W))
            {
                JCameraManager.ActiveCamera.Translate(new Vector3(0, 0, 1000f * (float)delta));
            }
            if (input.CurrentKeyboardState.IsKeyDown(Keys.S))
            {
                JCameraManager.ActiveCamera.Translate(new Vector3(0, 0, -1000f * (float)delta));
            }
            if (input.CurrentKeyboardState.IsKeyDown(Keys.D))
            {
                JCameraManager.ActiveCamera.Translate(new Vector3(-1000f * (float)delta, 0, 0));
            }
            if (input.CurrentKeyboardState.IsKeyDown(Keys.A))
            {
                JCameraManager.ActiveCamera.Translate(new Vector3(1000f * (float)delta, 0, 0));
            }
            if (input.CurrentMouseState.RightButton == ButtonState.Pressed)
            {
                JCameraManager.ActiveCamera.RotateX(input.MouseMoved.Y);
                JCameraManager.ActiveCamera.RotateY(input.MouseMoved.X);
            }
        }

        public override void Draw(GameTime gameTime)
        {
            base.Draw(gameTime);

            //JScreenManager.FadeBackBufferToBlack(TransitionAlpha * 2 / 3);

            //Viewport viewport = JEngineManager.GraphicsDevice.Viewport;

            //Rectangle fullscreen = new Rectangle(0, 0, viewport.Width, viewport.Height);

           // byte fade = TransitionAlpha;


            //draw background
            /*JScreenManager.SpriteBatch.Begin();
            JScreenManager.SpriteBatch.Draw(_background.BaseTexture as Texture2D,
                fullscreen,
                new Color(fade, fade, fade));
            JScreenManager.SpriteBatch.End();*/

            //draw ship
            JSceneGraphManager.Draw(gameTime);
        }

        public override void PostUIDraw(GameTime gameTime)
        {
            base.PostUIDraw(gameTime);
        }

        public void StartConsole()
        {
            JDropConsole.GUIPanel = _consolePanel;
            JDropConsole.BorderPadding = new Vector2(15, 15);
            //JDropConsole.BorderPadding = new Vector2(68, 68); //for non console panel gfx
            JDropConsole.LinePadding = 1.0f;
            JDropConsole console = new JDropConsole("Content/Font/console");
            JScreenManager.AddScreen(console);
        }

    }
}
